package com.caroil.caroilservice.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsets;

import com.caroil.caroilservice.R;
import com.caroil.caroilservice.view.TitleControlView;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.ycbjie.ycstatusbarlib.utils.StatusBarUtils;


/**
 * Fragment的基类
 */
public abstract class BaseFragment extends AbFragment {
    //获取TAG的fragment名称
    protected final String TAG = this.getClass().getSimpleName();
    public Context context;
    private Unbinder unbinder;

    @Override
    public void onAttach(Context ctx) {
        super.onAttach(ctx);
        context = ctx;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(initLayout(), container, false);
        ScreenAdapterTools.getInstance().loadView(rootView);
        unbinder = ButterKnife.bind(this, rootView);
        initView(rootView);
        initData(context);
        loadMethod();
        return rootView;

    }

    /**
     * 初始化布局
     *
     * @return 布局id
     */
    protected abstract int initLayout();

    /**
     * 初始化控件
     *
     * @param view 布局View
     */
    protected abstract void initView(final View view);

    /**
     * 初始化、绑定数据
     *
     * @param mContext 上下文
     */
    protected abstract void initData(Context mContext);

    /**
     * 加载方法
     */
    protected abstract void loadMethod();

    @Override
    public void onDestroyView() {
        unbinder.unbind();
        super.onDestroyView();
    }

    /**
     * 返回上一页
     *
     * @param activity
     */
    public void goBack(FragmentActivity activity) {
        if (activity.getSupportFragmentManager().getBackStackEntryCount() > 1) {
            pop();
        } else {
            ActivityCompat.finishAfterTransition(activity);
        }
    }

    /**
     * 设置标题头
     *
     * @param view
     */
    public void initTitle(View view, String title, FragmentActivity activity) {
        TitleControlView t = new TitleControlView(view, () -> goBack(activity));
        t.setTitle(title);
    }

    /**
     * 获取vm实例
     *
     * @return
     */
    public ViewModelProvider getViewModelProvider() {
        return new ViewModelProvider(this,
                ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication()));
    }


    /**
     * 设置顶部状态栏颜色和字体颜色
     */
    public void setBarState(boolean b) {
        try {
            Window window = getActivity().getWindow();
            View decorView = window.getDecorView();
            decorView.setOnApplyWindowInsetsListener((v, insets) -> {
                WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
                return defaultInsets.replaceSystemWindowInsets(
                        defaultInsets.getSystemWindowInsetLeft(),
                        b == true ? 0 : defaultInsets.getSystemWindowInsetTop(),
                        defaultInsets.getSystemWindowInsetRight(),
                        defaultInsets.getSystemWindowInsetBottom());
            });
            ViewCompat.requestApplyInsets(decorView);
            //将状态栏设成透明，如不想透明可设置其他颜色
            window.setStatusBarColor(ContextCompat.getColor(getContext(), b == true ? android.R.color.transparent : R.color.colorPrimary));
            //状态栏亮色模式，设置状态栏黑色文字、图标
            StatusBarUtils.StatusBarLightMode(getActivity());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
